﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WFL.Office.Word
{
    public class WordParagraph
    {
        private readonly object _lock = new object();

        private WordRange _range;
        public WordRange Range
        {
            get
            {
                if (_range == null)
                {
                    lock (_lock) { if (_range == null) { _range = new WordRange(_paragraph.Range) { Document = Document }; } }
                }
                return _range;
            }
        }

        public WordParagraphFormat Format
        {
            get { return new WordParagraphFormat(_paragraph.Format); }
            set { _paragraph.Format = value._paragraphFormat; }
        }

        private Paragraph _paragraph;

        public WordDocument Document
        {
            get;
            internal set;
        }

        internal WordParagraph(Paragraph paragraph)
        {
            _paragraph = paragraph;
        }

        public WordParagraph Next(int count)
        {
            object objCount = count;
            Paragraph p = _paragraph.Next(ref objCount);
            return p != null ? new WordParagraph(p) { Document = Document } : null;
        }

        public WordParagraph Next()
        {
            Paragraph p = _paragraph.Next();
            return p != null ? new WordParagraph(p) { Document = Document } : null;
        }
    }
}
